;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PlanRequireReport", [])
        .controller("PlanRequireReportCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };

            $scope.condition = {
                seller: true,
                createDateStart: ybWidget.FormatStartDate((new Date()).setDate(1)),
                createDateEnd: new Date()
            };
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1400,
                tableCol: [
                    {
                        name: "申请单号",
                        dataField: "purchaseNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "客户名称",
                        dataField: "customerName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "调度员",
                        dataField: "dispatcherName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "申请人",
                        dataField: "applicationName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "申请时间",
                        dataField: "applicationDateShow",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "审核人",
                        dataField: "realAuditName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "审核时间",
                        dataField: "realAuditDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "状态",
                        dataField: "statusDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "审核意见",
                        dataField: "auditNote",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "备注",
                        dataField: "note",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDesc",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "单位",
                        dataField: "unit",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "参考单价(￥)",
                        dataField: "price",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "申请数量",
                        dataField: "purchaseQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "可用库存",
                        dataField: "enableNum",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "合计(￥)",
                        dataField: "totalPrice",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "服务信息单号",
                        dataField: "serviceNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "是否返厂",
                        dataField: "isReturn",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "实际返厂",
                        dataField: "actualReturn",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "序列号",
                        dataField: "factoryNumber",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "已建出库数量",
                        dataField: "deliveryQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "实际出库数量",
                        dataField: "realQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "出库状态",
                        dataField: "deliveryStatus",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "出库时间",
                        dataField: "outDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "车型",
                        dataField: "prodType",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "车号",
                        dataField: "machineNumber",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "CRM单据号",
                        dataField: "crmOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "服务人员",
                        dataField: "serviceName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "服务网点",
                        dataField: "serviceSite",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "明细出库状态",
                        dataField: "detailStatusDesc",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "结算方式",
                        dataField: "settleTypeName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "三包类型",
                        dataField: "guaranteesTypeName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "收货人",
                        dataField: "receiverName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "手机号",
                        dataField: "receiverPhone",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "收货地址",
                        dataField: "receiverAddress",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "需求数量",
                        dataField: "requireQty",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                ]
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/threeGuaranteesPur/planRequireReportExport"),
                sectionParam: {billNos: [], materialsNos: []},
                allUrl: ybHttp.RequestUrl("device", "/threeGuaranteesPur/planRequireReportExport")
            };

            $scope.reset = function () {
                $scope.condition = {
                    seller: true,
                    createDateStart: ybWidget.FormatStartDate((new Date()).setDate(1)),
                    createDateEnd: new Date()
                };
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;

                var service = "device";
                var reqUrl = "/threeGuaranteesPur/planRequireReport";
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }

                for (var key in $scope.condition) {
                    if ($scope.condition[key] == "") {
                        $scope.condition[key] = undefined;
                    }
                }

                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.realAuditDateShow = $filter("YbDate")(item.realAuditDate, 'yyyy-MM-dd HH:mm:ss');
                        item.applicationDateShow = $filter("YbDate")(item.applicationDate, 'yyyy-MM-dd HH:mm:ss');
                        item.outDateShow = $filter("YbDate")(item.outDate, "yyyy-MM-dd HH:mm:ss");
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            function getBillIds() {
                $scope.exportParam.sectionParam = { billNos: [], materialsNos: [] };

                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.billNos.push(item.purchaseNo);
                        $scope.exportParam.sectionParam.materialsNos.push(item.materialsNo);
                    }
                });
            }

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
                getBillIds();
            };

            function getOrderStatues() {
                var service = "device";
                var reqUrl = "/dataDictionary/searchList";
                var param = {
                    parentCode: "SPZT"
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.billStateList = resp.value;
                })
            }

            (function () {
                getDataList();
                getOrderStatues();
            })();
        }])
    ;
})(window, window.angular);